<?php
App::uses('AppController', 'Controller');
/**
 * Downlad Controller
 *
 */
class DownloadController extends AppController {
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('download');
	}
	
	public function beforeRender(){
		parent::beforeRender();
	}
	
	public function download(){
		if(isset($this->params['file']) && !empty($this->params['file'])){
			$file_name = $this->params['file'];
		}
		
		if ($this->request->is('post')) {
						
			$this->loadModel('Newsletter');
			$this->Newsletter->set($this->request->data);
			$this->Newsletter->create();
			if($this->Newsletter->save($this->request->data)){
				
				$this->viewClass = 'Media';
				// Render app/webroot/files/example.docx
				preg_match("/\.([^\.]+)$/", $this->request->data['Newsletter']['file_name'], $matches);
				$params = array(
			        'id'        => $this->request->data['Newsletter']['file_name'],
			        'name'      => $matches[0],
			        'extension' => $matches[1],	        
			        'path'      => 'files' . DS
				);
				$this->set($params);
				$this->Session->delete('Download.file_name');
			}
		}else{
			$this->Session->write('Download.file_name',$file_name);
			$this->redirect($this->referer());
		}
	}
}